<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport"
          content="width=device-width; initial-scale=1; maximum-scale=1; minimum-scale=1; user-scalable=no;">
    <title>TS demo</title>
    <script src="https://cdn.bootcss.com/vConsole/3.2.0/vconsole.min.js"></script>
    <script src="https://unpkg.com/axios/dist/axios.min.js"></script>

    <script>
        // 初始化
        let vConsole = new VConsole();
        //console.log('Hello world');
    </script>
    <!-- 在html文档中 -->
    <script type="text/javascript"
            src="https://sf1-scmcdn-tos.pstatp.com/goofy/ee/lark/h5jssdk/lark/js_sdk/h5-js-sdk-1.5.6.js"></script>
</head>

<body>
<div id="bod">
    <button id="btn">分享</button>
    <button id="getLocation">获取地理位置</button>
    <input type="file" id="image" value="" >
    <a href="https://www.baidu.com">baidu</a>
    <!--<iframe src="https://h8sbodcroc.feishu.cn/docs/doccnR9jo2UFGENP6IkaY0cgchk"></iframe>-->
</div>
<script>
    window.onload = function () {
        // window.open('https://www.baidu.com?open_in_browser=true')
        // window.location.href = window.location.href + '?op_platform_service=hide_bavigator' https://open.feishu.cn/?op_platform_service=hide_navigator%27

        console.log(navigator.userAgent);

        const performance = window.performance || window.msPerformance || window.webkitPerformance;
        //console.log(performance.navigation.type);

        const btn = document.getElementById('btn');
        const getLocation = document.getElementById('getLocation');
        const recognizeCode = document.getElementById('recognizeCode');
        const image = document.getElementById('image');
        image.onchange = function (e) {
            const {files} = e.target;
            const file = files[0];
            console.log(file);
        };
        let appId, timestamp, nonceStr, signature;

        //第一次进入或刷新 的才做请求 回退到页面的 不请求 利用浏览器缓存即可
        if (performance.navigation.type === 0 || performance.navigation.type === 1) {
            // window.open('https://www.baidu.com')
            axios.get('/auth/v1/getAppAccessToken').then((res) => {
                axios.get('/auth/v1/authenSdk')
                    .then(function (response) {
                        console.log(response);
                        appId = response.data.data.appId;
                        timestamp = response.data.data.timestamp;
                        signature = response.data.data.signature;
                        nonceStr = response.data.data.noncestr;
                    })
                    .then(function () {
                        console.log(appId + ' ' + timestamp + ' ' + nonceStr + ' ' + signature);
                        // window.open('https://www.baidu.com')
                        window.h5sdk.ready(function () {
                            window.h5sdk.config({
                                appId, // 必填，应用ID
                                timestamp, // 必填，生成签名的时间戳，ms级
                                nonceStr, // 必填，生成签名的随机串
                                signature, // 必填，签名
                                jsApiList: [
                                    'scanCode'
                                ],
                                onSuccess: (res) => {
                                    console.log('鉴权结果：', res);
                                    console.log(`config: success ${JSON.stringify(res)}`);
                                    tt.getSetting({
                                        success (res) {
                                            console.log(res)
                                            // res.authSetting = {
                                            //   "scope.userInfo": true,
                                            //   "scope.userLocation": true
                                            // }
                                        }
                                    })
                                },
                                onFailed: (res) => {
                                    console.log(`config: fail ${JSON.stringify(res)}`, 'test');
                                },
                            });
                        });
                        window.h5sdk.error(err => {
                            // window.alert(JSON.stringify(err));
                            console.error('config error', JSON.stringify(err));
                        });
                    })
                    .catch(function (error) {
                        console.log(error);
                    });
            });

        }

        window.h5sdk.ready(() => {
            console.log(window.h5sdk);

            btn.addEventListener('click', function () {
                tt.downloadFile({
                    // 示例 url
                    url: 'http://10.93.220.249:3000/file/downloadFile',
                    success(res) {
                        const filePath = res.tempFilePath;
                        tt.openDocument({
                            filePath,
                            success(res) {
                                console.log('打开文档成功');
                            },
                        });
                    },
                });
            });


            getLocation.addEventListener('click', function (e) {
                tt.getLocation({
                    success(res) {
                        console.log(`经度 ${res.longitude}，纬度 ${res.latitude}`);
                    },
                    fail(res) {
                        console.log(`getLocation 调用失败`);
                    },
                });
            });


        });
    };
</script>
</body>

</html>
